package top.hkyzf.study.leetcode.array;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;

/**
 * @author 朱峰
 * @date 2022-1-14 8:27
 */
public class 查找和最小的K对数字_373 {
    public List<List<Integer>> kSmallestPairs(int[] nums1, int[] nums2, int k) {
        PriorityQueue<int []> pq = new PriorityQueue<>(k, (o1, o2) -> o1[0] + o1[1] - o2[0] - o2[1]);
        int max1 = Math.min(k, nums1.length);
        int max2 = Math.min(k, nums2.length);
        for (int i = 0; i < max1; i++) {
            for (int j = 0; j < max2; j++) {
                List<Integer> list = new ArrayList<>();
                int [] temp = new int[]{nums1[i], nums2[j]};
                pq.offer(temp);
            }
        }
        List<List<Integer>> pairs = new ArrayList<>();
        int max3 = Math.min(k, pq.size());
        for (int i = 0; i < max3; i++) {
            int[] ints = pq.poll();
            List<Integer> list = new ArrayList<>();
            list.add(ints[0]);
            list.add(ints[1]);
            pairs.add(list);
        }
        return pairs;
    }

    @Test
    public void testKSmallestPairs () {
        int [] nums1 = {1,7,11};
        int [] nums2 = {2,4,6};
        int k = 3;
        List<List<Integer>> smallestPairs = kSmallestPairs(nums1, nums2, k);
        System.out.println(smallestPairs);
    }
}
